﻿using ArticleClientLib;
using eslib.FormTools;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 软件文管理
{
    public partial class Login : Form
    {
        public Login()
        {
            new FormSizeAutoChanger(this);
            InitializeComponent();
        }

        private void Login_Load(object sender, EventArgs e)
        {

        }


        string user => textAdmin.Text.Trim();
        string pass => textPass.Text.Trim();


        void viewErr(string msg) => Invoke(() => labelErr.Text = msg);


        private void buttonLog_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pass))
            {
                viewErr("管理员账号/密码未填写");
                return;
            }

            buttonLog.Enabled = false;
            _ = login();

        }

        private async Task login()
        {
            try
            {
                ArticleApi api = new ArticleApi();
                var res = await api.Login(user, pass);
                if (!res)
                {
                    viewErr("管理员账号/密码不正确");
                    Invoke(() => buttonLog.Enabled = true);
                }
                else
                {
                    Invoke(() =>
                    {
                        ArticleApi.SetAdminAccount(user, pass);
                        UIDispach.ToStart();
                    });
                }
            }
            catch (Exception ex)
            {
                viewErr(ex.Message);
            }
        }

        private void buttonExit_Click(object sender, EventArgs e)
        {
            UIDispach.Exit();
        }
    }
}
